package nl.weeaboo.sh.level;

import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import nl.weeaboo.game.gl.GLImage;
import nl.weeaboo.game.gl.GLManager;
import nl.weeaboo.game.input.UserInput;
import nl.weeaboo.game.text.ParagraphRenderer;
import nl.weeaboo.game.text.StyledText;
import nl.weeaboo.game.text.TextStyle;
import nl.weeaboo.sh.Game;
import nl.weeaboo.sh.level.l99.Level99;

public class MainMenu extends AbstractLevel {

	private enum Screen { TITLE, OPTIONS };

	private Game game;
	private ParagraphRenderer pr;
	private GLImage bg;
	private Screen screen;
	
	public MainMenu(Game g) {
		super(g);
		
		this.game = g;
	}

	//Functions
	public void loadResources() {
		super.loadResources();
		
		setBackground(game.getImage("menu/title.png", true));
		screen = Screen.TITLE;
		
		pr = game.createParagraphRenderer();
	}
	public void unloadResources() {
		setBackground(null);
		
		super.unloadResources();
	}
	
	public void update(UserInput input, float dt) {		
		if (input.consumeKey(KeyEvent.VK_1)) {
			setScreen(null);
		} else if (input.consumeKey(KeyEvent.VK_2)) {
			setScreen(screen == Screen.TITLE ? Screen.OPTIONS : Screen.TITLE);
		} else {			
			if (input.consumeMouseButton(MouseEvent.BUTTON1)
					|| input.consumeKey(KeyEvent.VK_Z)
					|| input.consumeKey(KeyEvent.VK_X)
					|| input.consumeKey(KeyEvent.VK_C)
					|| input.consumeKey(KeyEvent.VK_SPACE)
					|| input.consumeKey(KeyEvent.VK_ENTER))
			{
				setScreen(null);
			}
		}
		
		if (screen == null) {
			//Start Game
			//getGame().setLevel(new Level1(getGame()));
			getGame().setLevel(new Level99(getGame()));
		}
	}

	public void drawBackground(GLManager glm, int w, int h) {
	}
	public void drawForeground(GLManager glm, int w, int h) {
		Game g = getGame();
		
		if (getBackground() != null) {
			getBackground().draw(glm, 0, 0, w, h);
		}
		
		if (screen == Screen.OPTIONS) {
			Font f = g.getDefaultFont();
			
			pr.setDefaultStyle(new TextStyle(f.getName(), f.getStyle(), 16));
			pr.setBounds(new Rectangle(100, 220, 640, 480-220));
			pr.setCursor(pr.getBounds().x, pr.getBounds().y);
			
			StyledText st = new StyledText("Options menu not implemented yet\nPress (2) to return to the menu");
			st.setStyle(new TextStyle(f.getName(), f.getStyle(), 16, 8));
			pr.drawText(glm, st);
		}
	}

	public boolean isForegroundOpaque() {
		return false;
	}

	//Getters
	protected GLImage getBackground() { return bg; }
	
	//Setters
	public void setBackground(GLImage image) { bg = image; }
	private void setScreen(Screen s) { screen = s; }
	
}
